package com.cinemate;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import com.cinemate.Entities.Updates;

import java.util.List;

public class HomePage extends Activity implements View.OnClickListener
{

    public static final String CINEMATE_PREFS = "Cinemate_Prefs";
    private static final int ALERT_EMPTY = 0;
    private static final int ALERT_INCORRECT = 1;
    private HttpUtil httputil;
    private SharedPreferences cinematePrefs;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        httputil = new HttpUtil();

        cinematePrefs = getSharedPreferences(CINEMATE_PREFS, 0);
        if (cinematePrefs.getBoolean("remember", false)){
            Intent loggedIn = new Intent();
            loggedIn.setClass(this, UpdatesList.class);
            startActivity(loggedIn);
        }

        Button login = (Button) findViewById(R.id.login);
        login.setOnClickListener(this);

    }

    public void onClick(View view) {

        CheckBox checkBox = (CheckBox) findViewById(R.id.remember);
        EditText login = (EditText) findViewById(R.id.username);
        EditText pass = (EditText) findViewById(R.id.password);
        switch (view.getId()){
            case R.id.login:
            {
                if (login.getText().toString().equals("") || login.getText() == null || pass.getText().toString().equals("") || pass.getText() == null){
                    showDialog(ALERT_EMPTY);
                }
                else {
                    String username = login.getText().toString();
                    String password = pass.getText().toString();

                    String passkey = httputil.LoginAndGetPasskey(username, password);
                    if (passkey.equals("")){
                        showDialog(ALERT_INCORRECT);
                    }
                    else {
                        if (checkBox.isChecked()){

                            SharedPreferences.Editor editor = cinematePrefs.edit();
                            editor.putBoolean("remember", true);
                            editor.putString("username", username);
                            editor.putString("password", password);
                            editor.putString("passkey", passkey);
                            editor.commit();
                        }

                        Intent loggedIn = new Intent();
                        loggedIn.setClass(this, UpdatesList.class);
                        startActivity(loggedIn);
                    }
                }

                break;
            }
        }
    }

    @Override
    public Dialog onCreateDialog(int id){

        switch (id){
            case ALERT_EMPTY:
                return new AlertDialog.Builder(this).setMessage(R.string.empty_auth).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int whichButton){
                    }
                }).create();
            case ALERT_INCORRECT:
                return new AlertDialog.Builder(this).setMessage(R.string.incorrect_auth).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int whichButton){
                    }
                }).create();
        }
        return null;
    }
}
